package com.example.demo.filter;

import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.factory.rewrite.RewriteFunction;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Slf4j
public class CacheRequestBodyFunction implements RewriteFunction<String, String> {

  @Override
  public Publisher<String> apply(ServerWebExchange exchange, String s) {
    // 在这里缓存一下，代替原 CacheRequestBody 功能, 因为 RemoveCachedBodyFilter 是默认开启的，这里不用删除该 key
    exchange.getAttributes().put(ServerWebExchangeUtils.CACHED_REQUEST_BODY_ATTR, s);
    return Mono.just(s);
  }
}
